package com.ruoyi.vpnClient.service.impl;

import com.ruoyi.vpn.domain.VpnUser;
import com.ruoyi.vpnClient.domain.Disconnect;
import com.ruoyi.vpnClient.mapper.VpnClientMapper;
import com.ruoyi.vpnClient.service.ClientService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
@Transactional(rollbackFor = Exception.class)
public class ClientServiceImp implements ClientService {
    @Resource
    private VpnClientMapper vpnClientMapper;
    @Override
    public boolean isOK(String username, String password) {

        //校验账号与密码是否匹配
        VpnUser vpnUser= vpnClientMapper.selectUserByUP(username,password);
        if (vpnUser !=null){
            if (vpnUser.getStatus() ==1){
                //设置用户状态为连接
                vpnUser.setConnectStatus(1L);
                HashMap<String, Object> map = new HashMap<>();
                map.put("username",username);
                map.put("connectStatus",1);
                vpnClientMapper.updateConnectStatusByUsername(map);
                return  true;
            }
        }

        return false;
    }
    @Override
    public void discount(Disconnect disconnect) {
        Long  allBandwidth=Long.parseLong(disconnect.getReceiveByte())+Long.parseLong(disconnect.getSendByte());
        Long allBandwidthM = allBandwidth/1024/1024;
        disconnect.setAllBandwidthM(allBandwidthM.toString());
//        System.out.println("disconnect = " + disconnect);
        HashMap<String, Object> map = new HashMap<>();
        map.put("username",disconnect.getUsername());
        map.put("connectStatus",0);
        vpnClientMapper.updateConnectStatusByUsername(map);
        vpnClientMapper.updataVpnUserByDisconnect(disconnect);
    }
}
